package com.exiassassin.librairies.game;

import java.io.Serializable;
import java.util.Vector;

@SuppressWarnings("serial")
public abstract class Player implements Serializable {

	protected int id;
	protected String login;
	protected String password;
	protected byte[] picture;
	protected boolean alive;
	protected Player[] targetsList;
	protected Coordinates coordinates;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public byte[] getPicture() {
		return picture;
	}
	public void setPicture(byte[] picture) {
		this.picture = picture;
	}
	public boolean getAlive() {
		return alive;
	}
	public void setAlive(boolean alive) {
		this.alive = alive;
	}
	public Player[] getTargetsList() {
		return targetsList;
	}
	public void setTargetsList(Player[] targetsList) {
		this.targetsList = targetsList;
	}
	public Coordinates getCoordinates() {
		return coordinates;
	}
	public void setCoordinates(Coordinates coordinates) {
		this.coordinates = coordinates;
	}
	
	public Player() {
	}
	public Player(String login, String password, byte[] picture) {
		this.login = login;
		this.password = password;
		this.picture = picture;
	}
	
	public Player(int id, String login, String password, byte[] picture,
			boolean isAlive, Player[] targetsList, Coordinates coordinates) {
		this.id = id;
		this.login = login;
		this.password = password;
		this.picture = picture;
		this.alive = isAlive;
		this.targetsList = targetsList;
		this.coordinates = coordinates;
	}
	
	public abstract Player denounceKiller(Player player);
	
	public abstract Player killPlayer(Player player, int radius);
	
	public static Player[] convertArrays(Vector<Player> playersVector)
	{
		Player[] player = new Player[playersVector.size()];
		for (int i = 0; i < playersVector.size(); i++)
			player[i] = playersVector.elementAt(i);
		return player;
	}
	
	public String toString()
	{
		String playerToString = this.id + ";" + this.login + ";" + this.password + ";" + this.alive + ";" + this.picture.toString();
		if (this.targetsList.length > 0)
		{
			playerToString += ";" + this.targetsList.length + ";";
			for (int i = 0; i < this.targetsList.length; i++)
			{
				playerToString += this.targetsList[i].toString() + ";";
			}
		}
		else 
			playerToString += ";0";
		return playerToString;
	}
	
	public void fromString(String stringPlayer)
	{
		String[] player = stringPlayer.split(";");
		this.id = Integer.parseInt(player[0]);
		this.login = player[1];
		this.password = player[2];
		this.alive = Boolean.parseBoolean(player[3]);
		this.picture = player[4].getBytes();
		int numberTargets = Integer.parseInt(player[5]);
		if (numberTargets > 0)
		{
			String target = "";
			for (int i = 6; i < player.length; i++ )
				target += player[i] + ";";
			
			for (int i = 0; i < numberTargets; i++)
			{
				this.targetsList[i].fromString(target);
			}
		}
	}
}
